<?php
##

//			 echo ' 0,'.$account_id.','.$customer_id.','.$pin.',\''.$local_phone.'\',
//			 			'.$payment_type.',\''.$transaction_id.'\','.$amount.','.$cc_type_id.',
//			 			\''.$cc_number.'\','.$cc_expirationdate.','.$cc_cvv.',
//			 			\''.$cc_firstname.'\','.$cc_lastname.','.$cc_address.',
//			 			\''.$cc_city.'\','.$cc_zipcode.','.$state_id;
//			 die();

##

class MyPayment{

	/**
     * Credit card processed by authorize.net
     * @return array result
	 */
	public static function CreditCardCharge_SP($model)
	{

		Yii::import('application.vendors.*');
		require_once('anet_php_sdk/AuthorizeNet.php');
			
		$transaction = new AuthorizeNetAIM(Yii::app()->params['AUTHORIZENET_API_LOGIN_ID'],Yii::app()->params['AUTHORIZENET_TRANSACTION_KEY']);
		$transaction->setSandbox(Yii::app()->params['AUTHORIZENET_SANDBOX']);
		$transaction->setFields(
		array(
	        'type'        => 'AUTH_CAPTURE',
	        'amount'      => $model->chargedAmount , 
	        'card_num'    => $model->creditCardNumber, 
	        'exp_date'    => $model->creditExpireMonth.$model->creditExpireYear ,
	        'card_code'   => $model->creditSecureCode,
	        'description' => 'From user : ' + Yii::app()->user->getState('username') + ' at site 0110.com' ,
	        'first_name'  => $model->cardHolderFirstName,
	        'last_name'   => $model->cardHolderLastName,
	        'address'     => $model->cardHolderAddress,
	        'city'        => $model->cardHolderCity,
	        'state'       => $model->cardHolderStateID  ,
	        'zip'         => $model->cardHolderZipCode,
	        'cust_id'     => Yii::app()->user->getState('customerID') ,
	        'customer_ip' => Yii::app()->request->userHostAddress , // sua sau
	        'invoice_num' => Yii::app()->user->getState('customerID') ,
		)
		);
		 
		$response = $transaction->authorizeAndCapture();
		if ($response->approved) {
			return array(
				'success'        => true,
				'transaction_id' => $response->transaction_id,
			);
//			$_return=self::ChargeAccount_SP(Yii::app()->user->AccountId,Yii::app()->user->id,$response->transaction_id,$model->CC_AMOUNT,1,Yii::app()->user->LocalPhone,$model);
//			return self::ReturnErrorMessage($_return,Yii::app()->user->AccountId,Yii::app()->user->id,$response->transaction_id);

		} else {
		  return array(
		  	'success' => false,
		  	'reason'  => $response->response_reason_text,
		  );
			
			//echo 'Location: error_page.php?response_reason_code='.$response->response_reason_code.'&response_code='.$response->response_code.'&response_reason_text=' .$response->response_reason_text;
			//return 'error='.$response->error.'&response_reason_code='.$response->response_reason_code.'&response_code='.$response->response_code.'&response_reason_text=' .$response->response_reason_text;
		}

	}

	/**
	  * I don't know     
 	  * @return 
	  */
	public static function ChargeAccount_SP($account_id,$customer_id,$transaction_id,$amount,$payment_type,$local_phone,$model=Null){
		$sql='EXEC BT_SP_ACCOUNT_UPDATE_BALANCE :AGENT_ID,:ACCOUNT_ID,:CUSTOMER_ID,:PIN,:PHONE_NUMBER,
												:PAYMENT_TYPE,:TRANSACTION_ID,:AMOUNT,:CC_TYPE_ID,
												:CC_NUMBER,:CC_EXPIRATIONDATE,:CC_CVV,:CC_FIRSTNAME,
												:CC_LASTNAME,:CC_ADDRESS,:CC_CITY,:CC_ZIPCODE,:STATE_ID';
			
			


		if($model != null){	
			$cc_type_id        = $model->CC_TYPE_ID;
			$cc_number         = $model->CC_NUMBER;
			$cc_expirationdate = $model->expirationMonth.$model->expirationYear;

			$cc_cvv      = $model->CC_CVV;
			$cc_firstname= $model->CC_FIRSTNAME;
			$cc_lastname = $model->CC_LASTNAME;
			$cc_address  = $model->CC_ADDRESS;
			$cc_city     = $model->CC_CITY;
			$cc_zipcode  = $model->CC_ZIPCODE;
			$state_id    = $model->STATE_ID;
		}else{
			$cc_type_id       = '';
			$cc_number        = '';
			$cc_expirationdate= '';
			$cc_cvv           = '';
			$cc_firstname     = '';
			$cc_lastname      = '';
			$cc_address       = '';
			$cc_city          = '';
			$cc_zipcode       = '';
			$state_id         = '';
				
		}

		$pin = Helper::generation_random_number(7);
			
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':AGENT_ID',Helper::param('AGENT_ID'),PDO::PARAM_INT);
		$command->bindParam(':ACCOUNT_ID',$account_id,PDO::PARAM_INT);
		$command->bindParam(':CUSTOMER_ID',$customer_id,PDO::PARAM_INT);
		$command->bindParam(':PIN',$pin,PDO::PARAM_STR);
		$command->bindParam(':PHONE_NUMBER',$local_phone,PDO::PARAM_STR);
		$command->bindParam(':PAYMENT_TYPE',$payment_type,PDO::PARAM_STR);
		$command->bindParam(':TRANSACTION_ID',$transaction_id,PDO::PARAM_STR);
		$command->bindParam(':AMOUNT',$amount,PDO::PARAM_STR);
		$command->bindParam(':CC_TYPE_ID',$cc_type_id,PDO::PARAM_INT);
		$command->bindParam(':CC_NUMBER',$cc_number,PDO::PARAM_STR);
		$command->bindParam(':CC_EXPIRATIONDATE',$cc_expirationdate,PDO::PARAM_STR);
		$command->bindParam(':CC_CVV',$cc_cvv,PDO::PARAM_STR);
		$command->bindParam(':CC_FIRSTNAME',$cc_firstname,PDO::PARAM_STR);
		$command->bindParam(':CC_LASTNAME',$cc_lastname,PDO::PARAM_STR);
		$command->bindParam(':CC_ADDRESS',$cc_address,PDO::PARAM_STR);
		$command->bindParam(':CC_CITY',$cc_city,PDO::PARAM_STR);
		$command->bindParam(':CC_ZIPCODE',$cc_zipcode,PDO::PARAM_STR);
		$command->bindParam(':STATE_ID',$state_id,PDO::PARAM_INT);

		$result=$command->queryRow();
			
		return $result;
	}


	/**
	  * Error message when process fail
	  * @return string Error result
	  */
	public static function ReturnErrorMessage($error,$account_id,$customer_id,$transaction_id){
		$message = null;

		switch($error['ERROR']){
			case 0 :
				$message['account_id']     = $error['ACCOUNT_ID'];
				$message['transaction_id'] = $error['TRANSACTION_ID'];
				$message['success']        = true;
				break;
			case 1:
				$message=Yii::t('message','Problem for update account. Please contact with customer services.');
				#$message = $error['MESSAGE'];
				Helper::send_mail_message(Helper::param('EMAIL_CS'),Helper::param('EMAIL_DEVELOPER'),'BATAM : ERROR FOR UPDATE ACCOUNT' , "BATAM: \n ACCOUNT ID : ".$account_id." \n CUSTOMER ID : ".$customer_id." \ TRANSACTION ID : ".$transaction_id." \n ERROR MESSAGE : ".$error['MESSAGE']);
				break;
			case 2 :
				$message=Yii::t('message','Problem for update account. Please contact with customer services.');
				#$message = $error['MESSAGE'];
				Helper::send_mail_message(Helper::param('EMAIL_CS'),Helper::param('EMAIL_DEVELOPER'),'BATAM : ERROR FOR UPDATE ACCOUNT' , "BATAM: \n ACCOUNT ID : ".$account_id." \n CUSTOMER ID : ".$customer_id." \ TRANSACTION ID : ".$transaction_id." \n ERROR MESSAGE : ".$error['MESSAGE']);
				break;
					
			case 3 :
			case 4 :
				$message=Yii::t('message','Problem for update account. Please contact with customer services.');
				Helper::send_mail_message(Helper::param('EMAIL_CS'),Helper::param('EMAIL_DEVELOPER'),'BATAM : ERROR FOR UPDATE ACCOUNT' , "BATAM: \n ACCOUNT ID : ".$account_id." \n CUSTOMER ID : ".$customer_id." \ TRANSACTION ID : ".$transaction_id." \n ERROR MESSAGE : ".$error['MESSAGE']);
				break;
					
			case 5 :
				$message=Yii::t('message','Problem for update account. Please contact with customer services.');
				Helper::send_mail_message(Helper::param('EMAIL_CS'),Helper::param('EMAIL_DEVELOPER'),'BATAM : ERROR FOR UPDATE ACCOUNT' , "BATAM: \n ACCOUNT ID : ".$account_id." \n CUSTOMER ID : ".$customer_id." \ TRANSACTION ID : ".$transaction_id." \n ERROR MESSAGE : ".$error['MESSAGE']);
				break;

		}

		return $message;
	}
}
